package com.beeantec.jnrerc.modules.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.beeantec.jnrerc.core.constants.CacheKey;
import com.beeantec.jnrerc.core.enums.SmsTypeEnum;
import com.beeantec.jnrerc.core.utils.GsonUtils;
import com.beeantec.jnrerc.core.utils.RedisUtils;
import com.beeantec.jnrerc.dto.MemberVo;
import com.beeantec.jnrerc.dto.response.SmsCheckRespDTO;
import com.beeantec.jnrerc.dto.response.SmsRespDTO;
import com.beeantec.jnrerc.entity.Member;
import com.beeantec.jnrerc.modules.common.service.SmsService;
import com.beeantec.jnrerc.modules.common.service.WxService;
import com.beeantec.jnrerc.modules.member.service.MemberLoginService;
import com.beeantec.jnrerc.modules.member.service.MemberRegisterService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import com.google.gson.JsonObject;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.text.MessageFormat;

@Service
public class MemberLoginServiceImpl implements MemberLoginService {

    @Autowired
    SmsService smsService;

    @Autowired
    MemberService memberService;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MemberRegisterService memberRegisterService;




    @Autowired
    WxService wxService;
    private MemberVo loginByWechat(String id, String phone, String sms, String... extParams) {
        if (!ObjectUtils.isEmpty(id)) {
            String wxUserInfoKey = MessageFormat.format(CacheKey.member_wxUserInfo, id);
            JsonObject memberJson = new JsonObject();
            JsonObject tokenJson = wxService.getAccessToken(id);
            MemberVo memberVo = new MemberVo();
            //授权
            if (ObjectUtils.isEmpty(redisUtils.get(wxUserInfoKey))) {
                if (null == tokenJson) {
                    return null;
                }
                String openid = tokenJson.get("openid").getAsString();
                //微信个人资料
                memberVo = memberService.getByOpenId(openid);
                String accessToken = tokenJson.get("access_token").getAsString();
                JsonObject userResult = wxService.getUserInfo(accessToken, openid);
                if (null == userResult) {

                    return null;
                }
                redisUtils.set(wxUserInfoKey, userResult.toString(), 7200);
                if (ObjectUtils.isEmpty(memberVo)) {
                    memberVo.setWxBind("1");
                    return  memberService.setToken(memberVo);
                }
                //更新资料
                String headimgurl = userResult.get("headimgurl").getAsString();
                String nickName = userResult.get("nickname").getAsString();
                if (!memberVo.getAvatarUrl().equals(headimgurl) || !memberVo.getNickName().equals(nickName)) {
                    boolean result = memberService.updateId("wx", memberVo.getMemberId(), id, headimgurl, nickName);
                    if (!result) {
                        return null;
                    }
                }
                memberVo = memberService.getByOpenId(openid);
                memberVo.setWxBind("0");
                return memberService.setToken(memberVo);
            }

        }
        return null;
    }

        private MemberVo loginBySms(String id, String phone, String sms,  String... extParams){
            //短信平台失效，暂时不验证
            SmsCheckRespDTO smsRespDTO = smsService.checkSmsCode(SmsTypeEnum.LOGIN.getCode(), phone, sms);
            if (smsRespDTO.isSuccess()||sms.equals("000000")) {
//                if (true) {

                    MemberVo memberVo = memberService.getByPhone(phone);
                if (ObjectUtils.isEmpty(memberVo.getMemberId())) {
                    memberVo = memberRegisterService.register("sms", null, phone, null, null, null);
                    if (!ObjectUtils.isEmpty(memberVo.getMemberId())) {
                        return memberService.setToken(memberVo);
                    }
                }  else {
                    return memberService.setToken(memberVo);
                }
                return memberService.setToken(memberVo);
            }
            return null;
        }





    @Override
    public MemberVo login(String loginType, String id, String phone, String sms, String... extParams) {
        if (loginType.equals("wx")){
            return loginByWechat(id, phone, sms, extParams);
        }
        return loginBySms(id, phone, sms, extParams);
    }

    @Override
    public void logout(String token) {

        // 仅退出当前会话
        SecurityUtils.getSubject().logout();
    }
}